package com.example.maruko_course_backend.convertor;

import com.example.maruko_course_backend.entity.NewWord;
import com.example.maruko_course_backend.vo.KnowledgeItemVo;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import static com.example.maruko_course_backend.utils.DisplayStringUtil.addEnterCharForDisplay;

@Component
public class KnowledgeWordVoConvertor extends KnowledgeVoConvertor {
  
  public List<KnowledgeItemVo> createWordsVoByEntity(List<NewWord> newWords, List<String> displayedTop10Words) {
    if (CollectionUtils.isEmpty(newWords)) {
      return new ArrayList<>();
    }
    List<KnowledgeItemVo> wordsVo = generateWordsVoByEntity(newWords);
    return filterToDisplayItemVo(wordsVo, displayedTop10Words);
  }
  
  public List<KnowledgeItemVo> generateWordsVoByEntity(List<NewWord> newWords) {
    List<KnowledgeItemVo> wordsVo = new ArrayList<>();
    for (int i = 0; i < newWords.size(); i++) {
      wordsVo.add(createWordVoByEntity(newWords.get(i), i));
    }
    return wordsVo;
  }
  
  private KnowledgeItemVo createWordVoByEntity(NewWord newWord, Integer i) {
    return KnowledgeItemVo.builder()
      .content(getDisplayWordContent(newWord))
      .seq(i)
      .build();
  }
  
  private String getDisplayWordContent(NewWord newWord) {
    String displayWordContent = newWord.getContent() + newWord.getPitch() + "-" + newWord.getMeaning();
    displayWordContent = appendCategory(displayWordContent, newWord.getCategory(), newWord.getIsSpecial());
    displayWordContent = addEnterCharForDisplay(displayWordContent);
    return displayWordContent;
  }
  
  private String appendCategory(String displayWordContent, String category, Boolean isSpecial) {
    if (category == null) {
      return displayWordContent;
    }
    return displayWordContent + "," + getCategory(category, isSpecial);
  }
  
  private String getCategory(String category, Boolean isSpecial) {
    return isSpecial ? "特殊" + category : category;
  }
}
